# -*- coding: utf-8 -*-
"""
2021-4-3

@author: 李运辰

公众号：python爬虫数据分析挖掘
"""
import requests
import time
import os
import random
import json
import time
import datetime
# 导入SnowNLP库
from snownlp import SnowNLP
# 统计排序
from collections import defaultdict
import operator
from stylecloud import gen_stylecloud 
import jieba

from flask_cors import *
from flask import Flask,render_template,request,Response,redirect,url_for
#内网ip
app = Flask(__name__)
CORS(app, supports_credentials=True)

headers = {
        
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
      }

###词云
def jieba_cloud(roomid):
    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()
    text_list = []
    # 当前
    #ymdhm = time.strftime("%Y-%m-%d", time.localtime())
    for i in msg_data:
        # if ymdhm in i:su
        text_list.append(i.split("*")[2])
    word_list = jieba.cut("".join(text_list))
    result = " ".join(word_list) #分词用 隔开

    #制作中文云词
    icon_name =['fas fa-dragon','fas fa-play','fas fa-video','fas fa-dog','fas fa-cat','fas fa-dove','fab fa-qq']
    timeStamp = random.randint(0, 6)
    #timeStamp = timeStamp.replace(".","").replace(" ","")
    picp=""+roomid+str(timeStamp)+'.png'
    gen_stylecloud(text=result,icon_name=icon_name[timeStamp],font_path='simsun.ttc',background_color="black",output_name=picp) #必须加中文字体，否则格式错误

    return picp

############################flask路由
#进入首页
@app.route('/')
def index():
    roomid = request.args.get('roomid')
    return render_template('analysis.html', roomid = roomid)

###弹幕列表
@app.route('/alldata')
def alldata():
    roomid = request.args.get('roomid')

    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()
    data_list={}
    data_list["d1"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    t_list = []

    if len(msg_data)>12:
        msg_data = msg_data[-12:]
    for i in range(len(msg_data)-1,-1,-1):
        t_list.append(msg_data[i])
    data_list['d2'] = t_list
    return Response(json.dumps(data_list), mimetype='application/json')

###不同时间点评论数分析
@app.route('/analyze1')
def analyze1():
    roomid = request.args.get('roomid')
    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()
    time_list = []
    #当前
    ymdhm = time.strftime("%Y-%m-%d %H", time.localtime())
    #ymdhm = "2021-04-02 22"
    h =  time.strftime("%H", time.localtime())
    #h = "22"
    for i in msg_data:
        if ymdhm in i:
            time_list.append(i.split("*")[0].split(":")[1])
    data_time = list(set(time_list))
    data_time.sort()
    ###只取三个
    if len(data_time)>7:
        data_time = data_time[-7:]

    name = [h+":"+i for i in data_time]
    value =[time_list.count(i) for i in data_time]

    data_list ={}
    data_list["d1"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    data_list['name'] = name
    data_list['value'] = value
    return Response(json.dumps(data_list), mimetype='application/json')

###情感判断分析
@app.route('/analyze2')
def analyze2():
    roomid = request.args.get('roomid')
    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()
    Sentiment_list = []
    ###正面
    count1 = 0
    ###负面
    count2 = 0
    for i in msg_data:
        # if ymdhm in i:
        text = i.split("*")[2]
        t = SnowNLP(text)
        if t.sentiments > 0.5:
            count1 = count1 + 1
        else:
            count2 = count2 + 1
    Sentiment_list.append(count1)
    Sentiment_list.append(count2)
    data_list = {}
    data_list["d1"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    data_list['d2'] = Sentiment_list
    return Response(json.dumps(data_list), mimetype='application/json')

###高频词统计
@app.route('/analyze3')
def analyze3():
    roomid = request.args.get('roomid')
    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()

    text_list = []
    # 当前
    # ymdhm = time.strftime("%Y-%m-%d %H", time.localtime())
    # ymdhm = "2021-04-02 22"
    # h =  time.strftime("%Y-%m-%d %H", time.localtime())
    # h = "22"
    for i in msg_data:
        # if ymdhm in i:
        text_list.append(i.split("*")[2])

    # 需要操作的句子
    text = "".join(text_list)
    text = text.replace("\n", "")
    s = SnowNLP(text)

    ###高频词统计
    list_all = s.words
    dict_x = {}
    for item in list_all:
        dict_x[item] = list_all.count(item)
    sorted_x = sorted(dict_x.items(), key=operator.itemgetter(1), reverse=True)
    re_word_list = []
    re_word_list_name = []
    re_word_list_data = []
    count = 0
    for k, v in sorted_x:
        if count == 5:
            break
        if len(k) > 1:
            re_word_list_name.append(k)
            re_word_list_data.append(v)
            count = count + 1
    re_word_list.append(re_word_list_name)
    re_word_list.append(re_word_list_data)

    return Response(json.dumps(re_word_list), mimetype='application/json')

###关键字抽取
@app.route('/analyze4')
def analyze4():
    roomid = request.args.get('roomid')
    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()
    text_list = []
    # 当前
    ymdhm = time.strftime("%Y-%m-%d %H", time.localtime())
    #ymdhm = "2021-04-02 22"
    h =  time.strftime("%H", time.localtime())
    #h = "22"
    for i in msg_data:
        if ymdhm in i:
            text_list.append(i.split("*")[2])
    # 需要操作的句子
    text = "".join(text_list)
    text = text.replace("\n", "")

    s = SnowNLP(text)
    key_word_list = s.keywords(limit=5)
    #print(key_word_list)
    key_word_dictlist=[]
    for i in key_word_list:
        t_sict = {}
        t_sict['value']=random.randint(5, 25)
        t_sict['name']=i
        key_word_dictlist.append(t_sict)

    data_list = {}
    data_list["d1"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    data_list['d2'] = key_word_dictlist
    return Response(json.dumps(data_list), mimetype='application/json')

###根据uid获取粉丝数和关注数
def get_data(uid):

    url = 'https://api.bilibili.com/x/relation/stat?vmid=%s&jsonp=jsonp'%(uid)
    r = requests.get(url)
    #print(r.json())
    #print(r.json()['data']["following"])
    #print(r.json()["data"]["follower"])
    return r.json()["data"]["follower"],r.json()['data']["following"]

###评论数
@app.route('/analyze5')
def analyze5():
    #531202562
    roomid = request.args.get('roomid')
    filename = roomid + ".txt"
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()

    data_list = {}
    data_list["d51"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


    ######获取up主uid
    url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory'
    headers = {
        'Host': 'api.live.bilibili.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
    }
    data = {
        'roomid': roomid,
    }
    html = requests.post(url=url, headers=headers, data=data).json()
    #print(html)

    uid = ""
    for i in html['data']['admin']:
        if len(i['medal']) > 0 and str(i['medal'][3]) == roomid:
            uid = i['medal'][-1]
            break;

    d52,d53 = get_data(uid)
    data_list['d52'] = d52
    data_list['d53'] = d53
    data_list['d54'] = len(msg_data)
    data_list['uid'] = uid
    return Response(json.dumps(data_list), mimetype='application/json')

###爬取弹幕
@app.route('/analyze6')
def analyze6():
    roomid = request.args.get('roomid')
    url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory'
    headers = {
        'Host': 'api.live.bilibili.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
    }
    data = {
        'roomid': roomid,
    }
    filename = roomid + ".txt"
    # 判断文件是否存在，不存在则创建
    if os.path.exists(filename):
        pass
    else:
        with open(roomid + ".txt", "a+", encoding='gb18030') as f:
            f.write("");
    ##############################
    ##读取当前txt
    with open(filename, "r", encoding='gb18030') as f:
        msg_data = f.readlines()
    html = requests.post(url=url, headers=headers, data=data).json()
    with open(filename, "a+", encoding='gb18030') as f:
        for content in html['data']['admin']:
            # 获取昵称
            nickname = content['nickname']
            # 获取发言
            text = content['text']
            # 获取发言时间
            timeline = content['timeline']
            # 记录发言
            msg = timeline + '*' + nickname + '*' + text
            ###不存在则添加
            if msg not in msg_data:
                f.write(str(msg) + "\n")
        for content in html['data']['room']:
            # 获取昵称
            nickname = content['nickname']
            # 获取发言
            text = content['text']
            # 获取发言时间
            timeline = content['timeline']
            # 记录发言
            msg = timeline + '*' + nickname + '*' + text
            ###不存在则添加
            if msg not in msg_data:
                f.write(str(msg) + "\n")
    return Response(json.dumps([]), mimetype='application/json')

#获取图片
@app.route('/find')
def find():
    roomid = request.args.get('roomid')
    picpath = jieba_cloud(roomid)
    data_list = {}
    data_list["picpath"] = picpath
    return Response(json.dumps(data_list), mimetype='application/json')

############################end
      
if __name__ == "__main__":    
    """初始化,debug=True"""
    app.run(host='172.30.0.16', port=5000,threaded=True)

